import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:rjpb/components/gap.dart';
import 'package:rjpb/components/z_button/z_button.dart';
import 'package:rjpb/components/z_modals/tip_dialog.dart';
import 'package:rjpb/routes/route.dart';
import 'package:rjpb/shared/extensions/extensions.dart';

export 'package:get/get.dart' hide Response, FormData, MultipartFile;

class Z {
  static Future<T?>? to<T>(String routeName, {dynamic arguments}) {
    final result = _getPage(routeName);

    return Get.to(
      result?.page,
      routeName: result?.name,
      arguments: arguments,
      preventDuplicates: true,
    );
  }

  static Future<T?>? offAll<T>(
    String routeName, {
    dynamic arguments,
    Transition? transition,
  }) {
    final result = _getPage(routeName);

    return Get.offAll(
      result?.page,
      arguments: arguments,
      routeName: result?.name,
      transition: transition,
      duration: 500.milliseconds,
    );
  }

  static void back<T>({
    T? result,
    bool closeOverlays = false,
    bool canPop = true,
    int? id,
  }) {
    return Get.back(
      result: result,
      closeOverlays: closeOverlays,
      canPop: canPop,
      id: id,
    );
  }

  static void unfocus([bool enforce = false]) {
    if (enforce) {
      Z.inputHide();
    }
    FocusManager.instance.primaryFocus?.unfocus();
  }

  static Future bottomSheet(
    Widget child, {
    Color? barrierColor,
  }) {
    return showModalBottomSheet(
      isScrollControlled: true,
      useSafeArea: true,
      enableDrag: false,
      context: Get.context!,
      builder: (_) => child,
      barrierColor: barrierColor,
      backgroundColor: Colors.transparent,
    );
  }

  static GetPage<dynamic>? _getPage(String routeName) {
    final result = Routes.pages.firstWhereOrNull(
      (page) => routeName == page.name,
    );
    return result;
  }

  static void toast(
    String msg, {
    Duration? displayTime,
  }) {
    SmartDialog.showToast(
      msg,
      debounce: true,
      displayTime: displayTime,
    );
  }

  static void inputHide() {
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }

  static void inputShow() {
    SystemChannels.textInput.invokeMethod('TextInput.show');
  }

  static Future showDialog({
    String? title,
    String? content,
    String? okText,
    String? cancelText,
    VoidCallback? ok,
    VoidCallback? cancel,
  }) {
    final context = Get.context!;

    return showCupertinoDialog(
        context: context,
        builder: (_) {
          return TipDialog(
            title: title,
            content: content,
            actions: [
              SizedBox(
                width: 117.w,
                child: ZButton(
                  text: cancelText ?? "取消",
                  buttonType: ButtonType.gray,
                  onPressed: () {
                    back();
                    cancel?.call();
                  },
                ),
              ),
              const Gap.w(20),
              SizedBox(
                width: 117.w,
                child: ZButton(
                  text: okText ?? "确定",
                  onPressed: () {
                    back(result: true);
                    ok?.call();
                  },
                ),
              ),
            ],
          );
        });
  }
}
